﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IList<VehicleType>>" %>
<%@ Import Namespace="Tencent.OA.ACM.Entity" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeaderPlaceHolder" runat="server">
<script type="text/javascript" language="javascript" src="<%=this.Url.Content("~/content/Scripts/MicrosoftAjax.js") %>"></script>
<script type="text/javascript" language="javascript" src="<%=this.Url.Content("~/content/Scripts/MicrosoftMvcValidation.js") %>"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainContainerPlaceHolder" runat="server">
<div class="title">
系统信息 -- 穿梭车辆
</div>
      <ul id="tags" class="tags">
        <li><a href='<%=this.Url.Content("~/Setting/Index") %>'>班车属性</a></li>
        <li ><a href='<%=this.Url.Content("~/Setting/City") %>'>城市信息</a></li>
        <li class="selectTag"><a href="javascript:void(0);">穿梭车辆</a></li>
        <li ><a href="<%=this.Url.Content("~/Setting/CacheRefresh") %>">缓存重建</a></li>
    </ul>
    <div class="tagCon">
        <div id="tagContent2" class="tagContent ">
        </div>
    </div>
   <%-- <div class="title ">
        统计报表
    </div>--%>
      <!--列表查询two-->
    <div class="list_title">
        <div class="pag_right">
        </div>
        <div style="padding-top: 6px;">
            <input type="button" value="新 增" class="form_bot oplink" onclick='newRow();' /> 
            <input type="button" value="保 存" class="form_bot" onclick='save();' />                   
            <%--   <input type="button" value="修改" class="form_bot" />
            <input type="button" value="删除" class="form_bot" />--%>
        </div>
    </div>
    <table id="myFault" class="tblData" cellpadding="0" cellspacing="0" style="width: 100%">
        <thead>
            <tr class="lt_head">
                <th style="width:120px">
                    序 号
                </th>
                <th>
                    车辆相关内容
                </th>               
                <th style="width:100px">
                    操 作
                </th>               
            </tr>
        </thead>
        <tbody id="gvList">
            <%
                if (this.Model != null && this.Model.Count() > 0)
                {
                    int counter = 1;
                    foreach (VehicleType item in this.Model)
                    {
                        %>
                         <tr>
                            <td>
                                <%=counter++ %>
                            </td>                           
                            <td style="text-align:left;">
                                穿梭车辆类型名称：<input type="text" maxlength="20" class="setting_field" value="<%=item.VehicleName %>" gid="<%=item.VehicleId %>"  style="width:487px;"/>
                                <br/>穿梭车辆类型说明：<textarea rows="5" cols="10" maxlength="1000" class="setting_field"  style="width:487px; text-align:left;"  gid="<%=item.VehicleId %>"><%=item.Remark %></textarea>
                            </td>
                            <td>
                                <a href="#"  class="oplink" onclick="remove($(this));">删除</a> 
                            </td>               
                        </tr>         
                        <%
                    }
                }
            %>
        
           
        </tbody>
    </table> 
    <%=Html.Hidden("DelRow") %>
    <script language="javascript" type="text/javascript">
        var delArray = new Array();
        var noNull = '<span class="required"><br/>* 穿梭车辆名称不能为空</span>';
        var remarkNotNull='<span class="required"><br/>* 穿梭车辆类型说明不能为空</span>';
        var noRepeat = '<span class="required"><br/>* 穿梭车辆类型名称不能重复</span>';
        var guidEmpty='<%=Guid.Empty %>';
        displayLoading = true;
        
         $(document).ready(function(){
            initFormState();
        });
        
        //删除行
        function remove(obj) {
            if (!confirm("是否确定删除该记录？")) {
                return;
            }   
            var id = obj.parent().parent().find("input")[0];
            var delId = id.attributes["gid"].value;
            if(delId==''){            
                removeRow(obj) 
                return;
            }  
            removeRow(obj); 
        }
        //移除行
        function removeRow(obj){
            var id = obj.parent().parent().find("input")[0];
            var delId = id.attributes["gid"].value;
            if (delId && delId != '') {
                // $("#DelRow").val($("#DelRow").val() + ',' + delId);
                delArray.push(delId);
            }
            obj.parent().parent().remove();
            refreshRowNumber();
        
        }
        //新增行
        function newRow() {
            var rows=$("#gvList tr");
            var html = '<tr><td>' + (rows.length +1)+ '</td>'
                      + ' <td style="text-align:left;">'
                      + ' 穿梭车辆类型名称：<input type="text" maxlength="20" class="setting_field" value="" gid="" style="width:487px;"/> '
                      + ' <br/>穿梭车辆类型说明：'
                      + '<textarea rows="5" cols="10" maxlength="1000" class="setting_field"  style="width:487px; text-align:left;"  gid=""></textarea>'
                      + ' </td> <td>'
                      + ' <a href="#" class="oplink"  onclick="remove($(this));">删除</a>  '
                      + ' </td></tr> ';
            $("#gvList").append(html);
        }
        //刷新行号
        function refreshRowNumber() {
            $("#gvList tr").each(function(i) {               
                $($(this).find("td")[0]).html(i+1);
            });
        }
        //检查输入
        function checkInput() {
            var isPass = true;
            var typeArr = new Array();
            removeAllErr();
            $("#gvList tr").each(function(i) {
                var val = $($(this).find("input")[0]).val();
                var remark=$($(this).find("textarea")[0]).val();
                $($(this).find("input")[0]).val(trim(val));
                if (trim(val) == '') {
                    $($(this).find("input")[0]).parent().append(noNull);
                    isPass = false;
                }
                else if(trim(remark)==''){
                    $($(this).find("input")[0]).parent().append(remarkNotNull);
                    isPass = false;
                }
                else {
                    var obj = new Object();
                    obj.name = val;
                    obj.row = i;
                    typeArr.push(obj);
                }
            });
            if(!isPass) 
                return false;
            //检查重复
            $("#gvList tr").each(function(i) {
                var val = $($(this).find("input")[0]).val();
                if (ifExist(val,i,typeArr) ) {
                    $($(this).find("input")[0]).parent().append(noRepeat);
                    isPass = false;                    
                    }
            });
            return isPass;
        }
        //清除错误提示
        function removeAllErr() {
            $("#gvList tr").each(function(i) {
                $(this).find("span").remove();
            });
        }
        //保存
        function save() {
            if(!checkInput() || !chekHtml()){
                return;
            }
            var postData = new Object();
            postData.DelIds = delArray;
            postData.TypeList = new Array();
            //postData.list.push();
            $("#gvList tr").each(function(i) {
                var input = $($(this).find("input")[0]);
                var remark = $($(this).find("textarea")[0]);
                var row = new Object();
                var typeDd=input.attr("gid");
                row.VehicleId = (typeDd=='')?guidEmpty:typeDd;
                row.VehicleName = input.val();
                row.Remark=remark.val(); 
                postData.TypeList.push(row);
            });
            //提交
             $.ajax({ 
                type: "POST",
                url: "<%=this.Url.Content("~/Setting/Vehicle") %>", 
                data: {"jsonString":Sys.Serialization.JavaScriptSerializer.serialize(postData)},
                success: function(msg){
                        if(msg.StatuCode==0)
                        {
                             resetFormState();
                             window.location.reload();
                        }                     
                    }
                });
        }
        //检查指定对象是否在数组中
        function ifExist(obj, pos, arr) {
        
            var ifExist = false;
            $.each(arr, function(n, o) {            
                if (obj == o.name && pos > o.row )
                    ifExist = true;
            });
            return ifExist;
        }
    </script>
</asp:Content>
